import { detail } from '../mutation-types'
import * as api from 'api/detail'
const state = {
  truckBasicInfo: {},
  truckImage: [],
  allImage: [],
  truckDealerInfo: [],
  truckParamInfo: {
    params: [],
    truckInfo: {},
  },
  compare_truckInfo: {
    params: [],
    truckInfo: {},
  },
  brandid: '',
  series_id: '',
  group_name_arr: {},
  dealerListInfo: []
}
const groupValue = (arr) => {
  var obj = {}, tmp = []
  arr.forEach((item) => {
    if (!obj[item.group_name]) {
      obj[item.group_name] = []
    } else {
      obj[item.group_name].push({
        name: item.param_name,
        value: item.param_value
      })
    }
  })
  for (var k in obj) {
    tmp.push({
      name: k,
      value: obj[k]
    })
  }
  return tmp
}

const resetValue = (obj) => {
  if (obj.params) {
    obj.params.forEach((item, index) => {
      item.value.forEach((k, v) => {
        k.value = ''
      })
    })
  }
  if (obj.truckInfo) {
    for (var k in obj.truckInfo) {
      obj.truckInfo[k] = ''
    }
  }
}

const actions = {
  getTruckBaseInfo ({ commit }, parmas) {
    return new Promise((resolve, reject) => {
      api.getTruckBaseInfo(parmas).then((data) => {
        commit(detail.GETTRUCKBASICINFO, data)
        resolve(data)
      })
    })
  },
  getTruckImage ({ commit }, parmas) {
    api.getTruckImage(parmas).then((data) => {
      console.log(data)
      commit(detail.GETTRUCKIMAGE, data)
    })
  },
  getTruckDealerInfo ({ commit }, parmas) {
    api.getTruckDealerInfo(parmas).then((data) => {
      commit(detail.GETTRUCKDEALERINFO, data)
    })
  },
  getTruckParamInfo ({ commit }, parmas) {
    let { type, truck_info_id } = parmas
    return new Promise((resolve, reject) => {
      api.getTruckParamInfo(parmas).then((data) => {
        commit(detail.GETTRUCKPARAMINFO, { data, type })
        // resolve(data)
      })
    })
  },
  getDealerListInfo ({ commit }, parmas) {
    api.getTruckDealerInfo(parmas).then(data => {
      commit(detail.GETTRUCKDEALERINFO, data)
    })
  },
  /**
   * 免费获取最低价
   */
  getLowestPrice ({commit}, param) {
    return new Promise((resolve, reject) => {
      api.getLowestPrice(param).then(data => {
        resolve()
      })
    })
  }
}

const mutations = {
  [detail.GETTRUCKBASICINFO] (state, data) {
    state.truckBasicInfo = data
  },
  [detail.GETTRUCKIMAGE] (state, data) {
    var tmp = data.imgs
    var arr = [], allImage = []
    tmp.forEach((item, index) => {
      arr[index] = {}
      arr[index].group_name = item.group_name
      arr[index].group_value = []
      if (item.group_value.length > 0) {
        item.group_value.forEach((itemImage) => {
          arr[index].group_value.push({ src: itemImage })
          allImage.push({ src: itemImage })
        })

      }
    })
    state.truckImage = arr
    state.allImage = allImage
  },
  [detail.GETTRUCKDEALERINFO] (state, data) {
    state.truckDealerInfo = data
  },
  [detail.GETTRUCKPARAMINFO] (state, res) {
    let { data, type } = res
    state.brandid = data.truckInfo.brand_id
    state.series_id = data.truckInfo.series_id
    var arr = [], obj = {}
    data.params.forEach((item) => {
      if (!obj[item.group_name]) {
        obj[item.group_name] = 1
        arr.push(item.group_name)
      }
    })
    state.group_name_arr = arr
    var params = groupValue(data.params)
    if (type == 0) {
      state.truckParamInfo.params = params
      state.truckParamInfo.truckInfo = data.truckInfo
    } else {
      state.compare_truckInfo.params = params
      state.compare_truckInfo.truckInfo = data.truckInfo
    }
  },
  [detail.RESETTRUCKPARAMINFO] (state) {
    state.truckParamInfo = {
      params: [],
      truckInfo: [],
    }
    state.compare_truckInfo = {
      params: [],
      truckInfo: {},
    }
  },
  [detail.EMPTYTRUCKPARAMINFO] (state, index) {
    if (index == 0) {
      resetValue(state.truckParamInfo)
    } else {
      resetValue(state.compare_truckInfo)
    }
  },
  [detail.GETDEALERLIST] (state, data) {
    state.truckDealerInfo = data
  }
}

const getters = {
  truckBasicInfo: (state) => state.truckBasicInfo,
  truckImage: (state) => state.truckImage,
  allImage: (state) => state.allImage,
  truckDealerInfo: (state) => state.truckDealerInfo,
  truckParamInfo: (state) => state.truckParamInfo,
  compare_truckInfo: (state) => state.compare_truckInfo,
  brandid: (state) => state.brandid,
  series_id: (state) => state.series_id,
  group_name_arr: (state) => state.group_name_arr,
  dealerListInfo: state => {
    return state.truckDealerInfo.map(item => ({key: item.id, value: item.name, inlineDesc: item.address}))
  }
}

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations
}
